package com.movecar.ui;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;

import com.movecar.R;
import com.movecar.utils.DataStore;
import com.movecar.utils.VersionManager;

/**
 * 框架
 * 
 * @author Zhao.JQ
 * 
 */
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {

	private TabHost tabHost;
	private TabHost.TabSpec spec;
	private Intent intent;
	private RelativeLayout navi_new_tip_3, navi_new_tip_5;
	RadioButton mRadioButton1;
	RadioButton mRadioButton2;
	RadioButton mRadioButton3;
	RadioButton mRadioButton5;
	private boolean push = false;
	private String newMessageId, newTrend, refereeDetail, csId, applyFriend;
	private long mExitTime;
	private int home_new_tip_2;
	private int home_new_tip_3;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// 获得SharedPreferences对象
		SharedPreferences userId = getSharedPreferences("userId", 0);
		// 获得可编辑对象
		SharedPreferences.Editor editor = userId.edit();
		editor.putString("userId", DataStore.getInstance().getUserId());
		editor.putString("mobile", DataStore.getInstance().getMobile());
		editor.commit();
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		// 读取数据
		final SharedPreferences preferences = getSharedPreferences("new_message", 0);

		newMessageId = getIntent().getStringExtra("newMessageId");
		newTrend = getIntent().getStringExtra("newTrend");
		refereeDetail = getIntent().getStringExtra("refereeDetail");
		applyFriend = getIntent().getStringExtra("applyFriend");
		push = getIntent().getBooleanExtra("push", false);
		final String moveCar = preferences.getString("moveCar", "");
		final String carOwner = preferences.getString("carOwner", "");

		navi_new_tip_3 = (RelativeLayout) findViewById(R.id.navi_new_tip_3);
		navi_new_tip_5 = (RelativeLayout) findViewById(R.id.navi_new_tip_5);
		mRadioButton1 = (RadioButton) this.findViewById(R.id.radio_button1);
		mRadioButton2 = (RadioButton) this.findViewById(R.id.radio_button2);
		mRadioButton3 = (RadioButton) this.findViewById(R.id.radio_button3);
		mRadioButton5 = (RadioButton) this.findViewById(R.id.radio_button5);

		if (!TextUtils.isEmpty(preferences.getString("applyFriend", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("newMessage", ""))) {
			navi_new_tip_3.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("newTrend", ""))) {
			home_new_tip_2 = 1;
		}
		if (!TextUtils.isEmpty(preferences.getString("messageInvite", ""))) {
			home_new_tip_3 = 1;
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-reward-share", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-reward-praise", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-reward-comment", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-reward-hotShare", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-exchange-recharge", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-exchange-getCash", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-exchange-join", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		if (!TextUtils.isEmpty(preferences.getString("coin-reward-redbag", ""))) {
			navi_new_tip_5.setVisibility(View.VISIBLE);
		}
		VersionManager vm = new VersionManager(this);
		vm.checkVersion();
		tabHost = this.getTabHost();
		if (push && !TextUtils.isEmpty(newMessageId)) {
			intent = new Intent().setClass(this, GroupActivity.class);
			intent.putExtra("push", push);
			intent.putExtra("newMessageId", newMessageId);
			spec = tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(intent);
			tabHost.addTab(spec);

			tabHost.setCurrentTab(3);
			mRadioButton3.setChecked(true);
		} else if (push && !TextUtils.isEmpty(newTrend)) {
			intent = new Intent().setClass(this, NewHomeActivity.class);
			spec = tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(intent);
			tabHost.addTab(spec);
			tabHost.setCurrentTab(1);
			mRadioButton1.setChecked(true);
		} else if (push && !TextUtils.isEmpty(refereeDetail)) {
			csId = getIntent().getStringExtra("csId");
			intent = new Intent().setClass(this, MoreActivity.class);
			intent.putExtra("push", push);
			intent.putExtra("csId", csId);
			spec = tabHost.newTabSpec("tab5").setIndicator("tab5").setContent(intent);
			tabHost.addTab(spec);
			tabHost.setCurrentTab(5);
			mRadioButton5.setChecked(true);
		} else if (push && !TextUtils.isEmpty(applyFriend)) {
			csId = getIntent().getStringExtra("csId");
			intent = new Intent().setClass(this, MoreActivity.class);
			intent.putExtra("push", push);
			intent.putExtra("applyFriend", true);
			spec = tabHost.newTabSpec("tab5").setIndicator("tab5").setContent(intent);
			tabHost.addTab(spec);
			tabHost.setCurrentTab(5);
			mRadioButton5.setChecked(true);
		} else {

			intent = new Intent().setClass(this, NewHomeActivity.class);
			intent.putExtra("contactFlag", getIntent().getStringExtra("contactFlag"));
			intent.putExtra("first_login", getIntent().getBooleanExtra("first_login", false));
			intent.putExtra("moveCar", moveCar);
			intent.putExtra("carOwner", carOwner);
			intent.putExtra("not_in", getIntent().getBooleanExtra("not_in", false));
			intent.putExtra("home_new_tip_2", home_new_tip_2);
			intent.putExtra("home_new_tip_3", home_new_tip_3);
			spec = tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(intent);
			tabHost.addTab(spec);
			tabHost.setCurrentTab(1);
		}

		RadioGroup radioGroup = (RadioGroup) this.findViewById(R.id.main_radio);

		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {

				switch (checkedId) {
				//
				case R.id.radio_button1:
					if (push) {
						intent = new Intent().setClass(MainActivity.this, NewHomeActivity.class);
						intent.putExtra("register", getIntent().getBooleanExtra("register", false));
						intent.putExtra("moveCar", moveCar);
						intent.putExtra("carOwner", carOwner);
						intent.putExtra("not_in", getIntent().getBooleanExtra("not_in", false));
						spec = tabHost.newTabSpec("tab1").setIndicator("tab1").setContent(intent);
						tabHost.addTab(spec);

					}
					tabHost.setCurrentTabByTag("tab1");
					break;
				//
				case R.id.radio_button2:

					intent = new Intent().setClass(MainActivity.this, ThemeActivity.class);
					spec = tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(intent);
					tabHost.addTab(spec);

					tabHost.setCurrentTabByTag("tab2");

					break;

				//
				case R.id.radio_button3:
					intent = new Intent().setClass(MainActivity.this, GroupActivity.class);
					spec = tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(intent);
					tabHost.addTab(spec);
					tabHost.setCurrentTabByTag("tab3");
					navi_new_tip_3.setVisibility(View.GONE);
					// 获得可编辑对象
					SharedPreferences.Editor editor = preferences.edit();
					editor.clear();
					editor.commit();
					break;

				case R.id.radio_button4:
					intent = new Intent().setClass(MainActivity.this, ClubActivity.class);
					spec = tabHost.newTabSpec("tab4").setIndicator("tab4").setContent(intent);
					tabHost.addTab(spec);

					tabHost.setCurrentTabByTag("tab4");
					break;

				case R.id.radio_button5:
					intent = new Intent().setClass(MainActivity.this, MoreActivity.class);
					intent.putExtra("notifyFlag", preferences.getString("applyFriend", ""));
					intent.putExtra("coin_reward_share", preferences.getString("coin-reward-share", ""));
					intent.putExtra("coin_reward_praise", preferences.getString("coin-reward-praise", ""));
					intent.putExtra("coin_reward_comment", preferences.getString("coin-reward-comment", ""));
					intent.putExtra("coin_reward_hotShare", preferences.getString("coin-reward-hotShare", ""));
					intent.putExtra("coin_reward_redbag", preferences.getString("coin-reward-redbag", ""));
					intent.putExtra("coin_exchange_recharge", preferences.getString("coin-exchange-recharge", ""));
					intent.putExtra("coin_exchange_getCash", preferences.getString("coin-exchange-getCash", ""));
					intent.putExtra("coin_exchange_join", preferences.getString("coin-exchange-join", ""));

					spec = tabHost.newTabSpec("tab5").setIndicator("tab5").setContent(intent);
					tabHost.addTab(spec);
					navi_new_tip_5.setVisibility(View.GONE);
					// 获得可编辑对象
					SharedPreferences.Editor editor2 = preferences.edit();
					editor2.clear();
					editor2.commit();
					tabHost.setCurrentTabByTag("tab5");

					break;

				default:
					// tabHost.setCurrentTabByTag("tab0");
					break;
				}
			}
		});
	}

	public static Boolean isExit = false;

	public boolean dispatchKeyEvent(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getRepeatCount() == 0) {

			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				isExit = true;
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {

				ShareSDK.stopSDK(this);
				System.exit(0);

				Intent exit = new Intent(Intent.ACTION_MAIN);
				exit.addCategory(Intent.CATEGORY_HOME);
				startActivity(exit);
				MainActivity.this.onDestroy();
				finish();

			}
			return false;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}

}
